/**
 * 用户管理js
 */
$(function () {
    initialPage();

    $.ajax({
        type: "POST",
        url: "/sys/user/userOrg",
        dataType: "json",
        success: function(result1){
            if(result1.code == 0){//登录成功
                getGrid(result1.data.orgId,result1.data.orgToken);
            }
        }
    });
});

function initialPage() {
    $(window).resize(function () {
        $('#dataGrid').bootstrapTable('resetView', {
            height: $(window).height() - 56
        });
    });
}

function getGrid(orgId,orgToken) {
    $('#dataGrid').bootstrapTable({
        url: 'http://api.sungohealth.com/mine/getUsers',
        height: $(window).height() - 56,
        dataField: "rows",
        method: 'post',
        contentType: "application/x-www-form-urlencoded",
        dataType: 'json',
        selectItemName: 'id',
        clickToSelect: true,
        pagination: true,
        smartDisplay: false,
        pageSize: 10,
        pageList: [10, 20, 30, 40, 50],
        paginationLoop: false,
        sidePagination: 'server',
        // showToggle:true,                    //是否显示详细视图和列表视图的切换按钮
        // cardView: true,
        // detailView: true,
        queryParamsType: null,
        onClickCell: function (field, value, row, $element) {
            dialogOpen1({
                url: '../../base/songo/healthInfo.html?_' + $.now(),
                width: '420px',
                height: '350px',
                success: function (iframeId) {
                    frames[iframeId].vm.user =row
                    if (frames[iframeId].vm.user.birthday.length>3){
                        frames[iframeId].vm.user.birthday =jsGetAge(frames[iframeId].vm.user.birthday)
                    }
                    frames[iframeId].vm.getDate();
                },

            });
            return false;
        },
        queryParams: function (params) {
            params.userId = 1;
            params.orgId = orgId;
            params.orgToken = orgToken;
            params.token = "e4642a8b1e41f43138f3c3db6ccabf95";
            params.search = vm.keyword
            return params;
        },
        columns: [{
            checkbox: true
        },
            {
                field: "userId",
                title: "编号",
                width: "50px"
            },
            {
                field: 'headPortrait', title: '用户头像',
                formatter: function (value, row, index) {
                    var s;
                    if (row.headPortrait != null) {
                        var url = row.headPortrait;
                        s = '<a class = "view"  href="javascript:void(0)">' +
                            '<img class="img-rounded m-t-xs img-responsive"  style="height: 50px;width: 50px" src="' + url + '" /></a>';
                    } else {
                        s = '<a class = "view"  href="javascript:void(0)">' +
                            '<img class="img-rounded m-t-xs img-responsive"  style="height: 50px;width: 50px" src="/statics/images/avatar.png" /></a>';
                    }
                    return s;
                },

                events: 'operateEvents'
            },
            {
                field: "nickname",
                title: "昵称",
                width: "200px"
            }, {
                field: "orgName",
                title: "所属机构",
                width: "200px"
            }, {
                field: "birthday",
                title: "生日",
                width: "300px"
            }, {
                field: "mobile",
                title: "手机号",
                width: "130px"
            }, {
                field: "sex",
                title: "性别",
                width: "60px"
                // formatter: function (value, row, index) {
                //     if (value == '0') {
                //         return '<span class="label label-danger">禁用</span>';
                //     } else if (value == '1') {
                //         return '<span class="label label-success">正常</span>';
                //     }
                // }
            }, {
                field: "addTime",
                title: "创建时间",
                width: "200px"
            }, {
                field: "remark",
                title: "备注"
            }],
        responseHandler: function (res) {
            // alert(JSON.stringify(res.data.userList[0]))
            return {
                total: res.data.allNumber,
                rows: res.data.userList
            };
        },
    })
}

var vm = new Vue({
    el: '#dpLTE',
    data: {
        keyword: null
    },
    methods: {
        load: function () {
            $('#dataGrid').bootstrapTable('refresh');
        },
        save: function () {
            dialogOpen({
                title: '新增用户',
                url: 'base/songo/add.html?_' + $.now(),
                width: '420px',
                height: '350px',
                scroll: true,
                yes: function (iframeId) {
                    top.frames[iframeId].vm.acceptClick();
                },
            });
        },
        edit: function () {
            var ck = $('#dataGrid').bootstrapTable('getSelections');
            if (checkedRow(ck)) {
                dialogOpen({
                    title: '编辑用户',
                    url: 'base/songo/edit.html?_' + $.now(),
                    width: '420px',
                    height: '350px',
                    scroll: true,
                    success: function (iframeId) {
                        top.frames[iframeId].vm.user = ck[0];
                        //  top.frames[iframeId].vm.setForm();
                    },
                    yes: function (iframeId) {
                        top.frames[iframeId].vm.acceptClick();
                    },
                });
            }
        },
        remove: function () {
            var ck = $('#dataGrid').bootstrapTable('getSelections'), ids = [];
            if (checkedArray(ck)) {
                $.each(ck, function (idx, item) {
                    ids[idx] = item.userId;
                });
                $.RemoveForm({
                    url: '../../sys/user/remove?_' + $.now(),
                    param: ids,
                    success: function (data) {
                        vm.load();
                    }
                });
            }
        },
        disable: function () {
            var ck = $('#dataGrid').bootstrapTable('getSelections'), ids = [];
            if (checkedArray(ck)) {
                $.each(ck, function (idx, item) {
                    ids[idx] = item.userId;
                });
                $.ConfirmForm({
                    msg: '您是否要禁用所选账户吗？',
                    url: '../../sys/user/disable?_' + $.now(),
                    param: ids,
                    success: function (data) {
                        vm.load();
                    }
                });
            }
        },
        enable: function () {
            var ck = $('#dataGrid').bootstrapTable('getSelections'), ids = [];
            if (checkedArray(ck)) {
                $.each(ck, function (idx, item) {
                    ids[idx] = item.userId;
                });
                $.ConfirmForm({
                    msg: '您是否要启用所选账户吗？',
                    url: '../../sys/user/enable?_' + $.now(),
                    param: ids,
                    success: function (data) {
                        vm.load();
                    }
                });
            }
        },
        reset: function () {
            var ck = $('#dataGrid').bootstrapTable('getSelections');
            if (checkedRow(ck)) {
                dialogOpen({
                    title: '重置密码',
                    url: 'base/user/reset.html?_' + $.now(),
                    width: '400px',
                    height: '220px',
                    success: function (iframeId) {
                        top.frames[iframeId].vm.user.userId = ck[0].userId;
                    },
                    yes: function (iframeId) {
                        top.frames[iframeId].vm.acceptClick();
                    },
                });
            }
        }
    }
})

function jsGetAge(strBirthday) {
    var returnAge;
    var strBirthdayArr = strBirthday.split("-");
    var birthYear = strBirthdayArr[0];
    var birthMonth = strBirthdayArr[1];
    var birthDay = strBirthdayArr[2];

    d = new Date();
    var nowYear = d.getFullYear();
    var nowMonth = d.getMonth() + 1;
    var nowDay = d.getDate();

    if(nowYear == birthYear) {
        returnAge = 0; //同年 则为0岁
    } else {
        var ageDiff = nowYear - birthYear; //年之差
        if(ageDiff > 0) {
            if(nowMonth == birthMonth) {
                var dayDiff = nowDay - birthDay; //日之差
                if(dayDiff < 0) {
                    returnAge = ageDiff - 1;
                } else {
                    returnAge = ageDiff;
                }
            } else {
                var monthDiff = nowMonth - birthMonth; //月之差
                if(monthDiff < 0) {
                    returnAge = ageDiff - 1;
                } else {
                    returnAge = ageDiff;
                }
            }
        } else {
            returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
        }
    }

    return returnAge; //返回周岁年龄

}
